package com.atbun.aclservice.controller;


import com.atbun.aclservice.entity.AclPermission;
import com.atbun.aclservice.service.PermissionService;
import com.atbun.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    /**
     * 获取全部菜单
     * @return
     */
    @GetMapping()
    public R indexAllPermission(){
        List<AclPermission> permissionList = this.permissionService.indexAllPermission();
        return R.ok().data("children",permissionList);
    }

    /**
     * 根据id递归删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("deletePermission/{id}")
    public R deletePermission(@PathVariable String id){
        this.permissionService.deletePermission(id);
        return R.ok().message("该"+id+"下的所有菜单删除成功");
    }

    /**
     * 给角色分配权限
     * @param roleId
     * @param permissionId
     * @return
     */
    @PostMapping("doAssign")
    public R doAssign(String roleId,String[] permissionId){
        this.permissionService.saveRolePermissionRealtionShip(roleId,permissionId);
        return R.ok();
    }

    /**
     * 根据角色id获取菜单
     * @param roleId
     * @return
     */
    @GetMapping("toAssign/{roleId}")
    public R toAssign(@PathVariable String roleId){
        List<AclPermission> list = this.permissionService.selectAllMenu(roleId);
        return R.ok().data("children",list);
    }

    /**
     * 新增菜单
     * @param permission
     * @return
     */
    @PostMapping("insertPermission")
    public R insertPermission(@RequestBody AclPermission permission){
        this.permissionService.save(permission);
        return R.ok();
    }

    /**
     * 修改菜单
     * @param permission
     * @return
     */
    @PutMapping("updatePermission")
    public R updatePermissionById(@RequestBody AclPermission permission) {
        permissionService.updateById(permission);
        return R.ok();
    }
}

